import sys
import json
import pickle
import os

sys.path.append(r'./')

from codesecurity.feature.objects import CommonFeatureSet
from codesecurity.feature.common_metrics import count_node_type_js
import numpy as np

def prepare_obfuscate_files():
    obfuscate_levels=['light','medium','heavy']

    choose_files=['bad/127d09ef46a3e8c56e02812c630ab3ab_invoice_scan_fQaneK.js',
                'good/0fe14a4bd709031d1af4798ec62e351b426597e7.js',
                'good/0e164c3ab74ca28535a7c4f55192cb9c4e1c0f16.js']
    
    obfuscate_file=f'/home/passwd123/XiaoweiGuo/vscode/code-security/data/js/obfucscate/{{}}/jstap_train/{choose_file}'
    
    group=[]
    
    for choose_file in choose_files:
        test_files=[f'/home/passwd123/XiaoweiGuo/vscode/code-security/data/js/jstap_train/{choose_file}']
        test_files+=[obfuscate_file.format(e) for e in obfuscate_levels]
        group.append(test_files)
        
    return group

def prepare_normal_files():
    import random
    group=[]
    
    target_dir='data/js/jstap_train/good'
    
    normal_files=[os.path.join(target_dir,e) for e in os.listdir(target_dir)]

    group.append(random.sample(normal_files,50))
    
    return group

def prepare_good_good_pairs(repeat=1,obsfucate_level=None):
    import random
    group=[]
    
    if obsfucate_level is None:
        target_dir='data/js/jstap_train/good'
    else:
        target_dir=f'data/js/obfucscate/{obsfucate_level}/jstap_train/good'
    
    normal_files=[os.path.join(target_dir,e) for e in os.listdir(target_dir) if e.endswith('.js')]

    for i in range(repeat):
        random.shuffle(normal_files)
        for i in range(len(normal_files)-1):
            group.append([normal_files[0],normal_files[i+1]])
    
    return group

def prepare_bad_bad_pairs(repeat=1):
    import random
    group=[]
    
    target_dir='data/js/jstap_train/bad'
    
    unormal_files=[os.path.join(target_dir,e) for e in os.listdir(target_dir)]

    for i in range(repeat):
        random.shuffle(unormal_files)
        for i in range(len(unormal_files)-1):
            group.append([unormal_files[0],unormal_files[i+1]])
    
    return group

def prepare_good_bad_pairs(repeat=1):
    import random
    group=[]
    
    target_good_dir='data/js/jstap_train/good'
    target_bad_dir='data/js/jstap_train/bad'
    
    good_files=[os.path.join(target_good_dir,e) for e in os.listdir(target_good_dir)]
    bad_files=[os.path.join(target_bad_dir,e) for e in os.listdir(target_bad_dir)]
    
    number=repeat
    
    for i in range(number):
        random.shuffle(good_files)
        random.shuffle(bad_files)
        
        for good_file,bad_file in zip(good_files,bad_files):
            group.append([good_file,bad_file])
    
    return group

def prepare_obfuscate_good_bad_pairs(repeat=1,obfuscate_level:['light','medium','heavy']='light'):
    import random
    group=[]
    
    target_good_dir=f'data/js/obfucscate/{obfuscate_level}/jstap_train/good'
    target_bad_dir=f'data/js/obfucscate/{obfuscate_level}/jstap_train/bad'

    good_files=[os.path.join(target_good_dir,e) for e in os.listdir(target_good_dir) if e.endswith('.js')]
    bad_files=[os.path.join(target_bad_dir,e) for e in os.listdir(target_bad_dir) if e.endswith('.js')]
    
    number=repeat
    for _ in range(number):
        random.shuffle(good_files)
        random.shuffle(bad_files)
        
        for good_file,bad_file in zip(good_files,bad_files):
            group.append([good_file,bad_file])
            
    return group
group=prepare_good_good_pairs(repeat=1,obsfucate_level='light')

group_vectors=[]

simlarities_total=[]

for test_files in group:
    vectors=[]
    for test_file in test_files:
        features=CommonFeatureSet.from_file(test_file)
        vectors.append(count_node_type_js(features.ast_object)) 

    vectors=[e/np.linalg.norm(e) for e in vectors]
    simlarities=[np.sum(vectors[0]*e) for e in vectors[1:]]
    simlarities_total+=simlarities
    print(simlarities)
    
print(np.mean(simlarities_total))

# with open('table_data/obfu_freq.pkl','wb') as f:
#     pickle.dump(vectors,f)